#include "CountActivity.h"
#include<QLabel>
#include <QPushButton>
#include <QLayout>
#include <QMessageBox>

CountActivity::CountActivity(QWidget *parent, Bridget *bd):
        QWidget(parent), h(0), m(0), s(0), setStatus(false)
{

    act = new Act(bd);
    setStatusButton.setVisible(false);

    countTimer = new QTimer(this);
    countTimer2 = new QTimer(this);


    ltimer = new QLabel(this);
    ltimer->setVisible(false);
    activity = new QComboBox(this);





    QDomDocument docAct;
    QFile file("baseAct.xml");
    if (file.open(QIODevice::ReadOnly))
    {
        if (docAct.setContent(&file))
        {
            QDomElement root = docAct.documentElement();
            QDomElement child = root.firstChildElement("act");
            while (!child.isNull())
            {
                activity->addItem(child.attribute("name", ""));
                child = child.nextSiblingElement("act");

            }

        }

     file.close();
     }


    activity->setEditable(true);
    activity->clearEditText();

    curActivity = new QLabel(this);
    curActivity->setVisible(false);
    activity->setVisible(true);
    stopButton.setText("Stop");
    stopButton.setVisible(false);
    setStatusButton.setText("Set status");
    setStatusButton.setVisible(true);

    QLabel *statusLbl = new QLabel(tr("Status:"),this);

    QHBoxLayout* lh1 = new QHBoxLayout;
    QHBoxLayout* lh2 = new QHBoxLayout;
    QVBoxLayout* mainLayout = new QVBoxLayout(this);
    lh1->addWidget(statusLbl);
    lh1->addWidget(activity);
    lh1->addWidget(curActivity);

    lh2->addWidget(ltimer);
    lh2->addWidget(&stopButton);
    lh2->addWidget(&setStatusButton);

    mainLayout->addLayout(lh1);
    mainLayout->addLayout(lh2);


    connect(countTimer, SIGNAL(timeout()), SLOT(slotUpdateTimer()));
    //connect(countTimer2, SIGNAL(timeout()), SLOT(slotUpdateAct()));
    connect (&stopButton, SIGNAL(clicked()), SLOT(onStopButtonClicked()));
    connect (&setStatusButton, SIGNAL(clicked()), SLOT(onSetStatusButton()));

}

//If timer is run return true else return false
bool CountActivity::getSetStatus()
{
    return setStatus;
}

void CountActivity::slotUpdateTimer()
{

    if (s<59)
    {
        s++;
     }
    else
        if (m<59)
        {
            m++;
            s=0;
           }
            else
            {
                h++;
                m=0;
                s=0;
            }
            curTime.setHMS(h,m,s);

       QString str= curTime.toString("hh:mm:ss");
        ltimer->setText(str);
}
void CountActivity::onStopButtonClicked()
{
    setStatus=false;
    countTimer->stop();
    countTimer2->stop();
    /* Секундами пренебрегаем, записи о минутах уже существуют*/
    double time = (double)(h*60+m+(double)s/60);
    //double time = (double)s/60;//h*60+m+1;
    qDebug()<<"time"<<time;
    act->setTime(time);
    if (act->setPar())
    {

        act->doRecordAct();
        QMessageBox::information(this, tr("Bridget"), tr("The action record added in journal."));

    }


    stopButton.setVisible(false);
    setStatusButton.setVisible(true);
    activity->setVisible(true);
    ltimer->setVisible(false);
    curActivity->setVisible(false);

    activity->setEditable(true);
    activity->clearEditText();


}

void CountActivity::onSetStatusButton()
{
    setStatus=true;
    act->setName(activity->currentText());
    if ((activity->currentText().length()==0) && (!act->inBase()))
    {
        QMessageBox::information(this, tr("Bridget"), tr("The enter data isn't correct!"));
        return;
    }
    else
    {
    ltimer->setText("00:00:00");
    ltimer->setStyleSheet("QLabel {border: 2px solid green;}");
    h=0; m=0; s=0;
    curTime.setHMS(h,m,s);

    countTimer->start(1000);
    countTimer2->start(60000);
    ltimer->setVisible(true);
    setStatusButton.setVisible(false);
    stopButton.setVisible(true);
    curActivity->setText(activity->currentText());
    curActivity->setVisible(true);
    activity->setVisible(false);
}
}

double CountActivity::getEnergy()
{
   double energy=0;
   if (getSetStatus())
   {
   double time = h*60+m+s/60;
   qDebug()<<"sec="<<s;
   act->setTime(time);
   if (act->setPar())
   {

       energy=act->getEnergy();

   }
}
   return energy;
}

void CountActivity::slotUpdateAct()
{
   act->setTime(1);
        if (act->setPar())
        {

            act->doRecordAct();
            //QMessageBox::information(this, tr("Bridget"), tr("The action record added in journal."));

        }

}
